//获取应用实例
var app = getApp();
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    isAgree: false,
    modalIsHint: true,
    indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_1.jpg",
    hostUrl: app.globalData,
    isOnTime: app.globalData.isOnTime,
    isSucceed: true,//app.globalData.isSucceed,
    isPunchHint: true,//app.globalData.isPunchHint,
    isPunchCardResultHid: false, // 显示下方打卡的结果
    isHint: true,
    PuCard: true,
    DoCard: true,
    PaCard: true
  },

  onLoad: function () {

    console.log('onLoad')

  },

  onShow: function () {

    console.log("看看分享的走没走这个方法0" + "++++++++++++++++");

    var that = this;
    var currentTime;
    clearInterval(app.globalData.timeInterval);

    wx.showLoading({
      title: '请稍后'
    })
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          height: res.windowHeight,
          width: res.windowWidth,
          modalIsHint: wx.getStorageSync("modalIsHint")
        })
      }
    })

    app.request("particiCallenge", {}, true).then(function (data) {
      console.log(data);
      wx.hideLoading()
      var t = data.data.nowtime;
      currentTime = t.split(" ")[1]//"09:00:00";
      var netHour = currentTime.split(":")[0].valueOf(); // 时
      var netMinte = currentTime.split(":")[1].valueOf(); // 分
      var netSecond = currentTime.split(":")[2].valueOf(); // 秒
      var utoday = data.data.user.utoday == 1;//今天是否参数挑战 1参与0未参与
      var utoday_is_close = data.data.user.utoday_is_close == 1; // 今天的是否大卡
      var utomorrow = data.data.user.utomorrow == 1;// 是否参加了挑战（是否支付） 1参与0未参与
      var utoday_status = data.data.user.utoday_status == 1;// 今天的打卡状态
      app.globalData.startTime = data.data.particiDate.startDate;
      app.globalData.endTime = data.data.particiDate.endDate;
      console.log("看看分享的走没走这个方法1" + "++++++++++++++++");
      that.setData({
        homeData: data.data,
        failCount: data.data.todaycallenge.call_num - data.data.todaycallenge.call_sign_num, // 失败的人数
        currentTime: currentTime, // 获取当前时间
        utomorrow: utomorrow, // 是否参加了明天挑战（是否支付） 1参与0未参与
        utoday: utoday,//是否参与了今天挑战 1参与0未参与
        utoday_is_close: utoday_is_close, // 今天的是否打卡
        xStartTime: app.globalData.startTime, // 开始时间
        xEndTime: app.globalData.endTime,  // 结束时间
        userLength: (7 - data.data.tomorrowcallenge.user.length) * 2 + 35,// 数组的大小
        userSize: data.data.tomorrowcallenge.user.length,

      });
      console.log("看看分享的走没走这个方法2" + "++++++++++++++++" + that.data.userLength);
      // 打卡时间段
      if (currentTime >= app.globalData.startTime && currentTime < app.globalData.endTime) {
        // 参加了今天
        console.log("看看分享的走没走这个方法3" + "++++++++++++++++");
        if (utoday) {
          console.log("看看分享的走没走这个方法4" + "++++++++++++++++");
          // 打过卡
          if (utoday_is_close) {
            console.log("看看分享的走没走这个方法5" + "++++++++++++++++");
            //参加了明天的
            if (utomorrow) {
              console.log("看看分享的走没走这个方法6" + "++++++++++++++++");
              var diffHour = 23 + parseInt(app.globalData.startTime.split(":")[0]) - netHour;
              var diffMinute = 59 - netMinte;
              var diffSecond = 60 - netSecond;
              that.startCountDown(diffHour, diffMinute, diffSecond);

              that.setData({
                isOnTime: true,
                PuCard: true,
                DoCard: false,
                PaCard: true,
                indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_1.jpg"
                // indexBackgroundImgUrl:"/images/bg_1.jpg"
              });

            } else {//没有参加明天的
              console.log("看看分享的走没走这个方法8" + "++++++++++++++++");
              that.setData({
                indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_1.jpg",
                isOnTime: true,
                PuCard: true,
                DoCard: true,
                PaCard: false
              });
            }
          } else {//没有打卡
            console.log("看看分享的走没走这个方法9" + "++++++++++++++++");
            that.setData({
              isOnTime: true,
              PuCard: false,
              DoCard: true,
              PaCard: true,
              indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_0.jpg"
              // indexBackgroundImgUrl: "/images/bg_2.jpg"
            });
          }
        } else {//没有参加今天
          console.log("看看分享的走没走这个方法10" + "++++++++++++++++");
          if (utomorrow) { // 参加了明天的
            console.log("看看分享的走没走这个方法6" + "++++++++++++++++");
            var diffHour = 23 + parseInt(app.globalData.startTime.split(":")[0]) - netHour;
            var diffMinute = 59 - netMinte;
            var diffSecond = 60 - netSecond;
            that.startCountDown(diffHour, diffMinute, diffSecond);
            that.setData({
              isOnTime: true,
              PuCard: true,
              DoCard: false,
              PaCard: true,
              indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_1.jpg"
              // indexBackgroundImgUrl:"/images/bg_1.jpg"
            });
          } else { // 没有参加明天的
            that.setData({
              isOnTime: true,
              PuCard: true,
              DoCard: true,
              PaCard: false,
              indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_1.jpg"
              // indexBackgroundImgUrl: "/images/bg_1.jpg"
            });
          }

        }
      } else { // 不是打卡时间段
        console.log("看看分享的走没走这个方法11" + "++++++++++++++++");
        if (currentTime < app.globalData.startTime) { // 0点到打卡前+++++++++++++++++++++++++
          console.log("看看分享的走没走这个方法20" + "++++++++++++++++");
          if (utoday) { // 参加今天的
            console.log("看看分享的走没走这个方法21" + "++++++++++++++++");
            if (!utoday_is_close || !utoday_status) { // 没有打卡
              var diffHour = -1 + parseInt(app.globalData.startTime.split(":")[0]) - netHour;
              var diffMinute = 59 - netMinte;
              var diffSecond = 60 - netSecond;
              that.startCountDown(diffHour, diffMinute, diffSecond);
              console.log("看看分享的走没走这个方法22" + "++++++++++++++++");
              that.setData({
                isOnTime: false,
                PuCard: true,
                DoCard: false,
                PaCard: true,
                indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_0.jpg"
                // indexBackgroundImgUrl: "/images/bg_1.jpg"
              });
            }
          } else { // 没有参见今天的
            if (utomorrow) {
              console.log("看看分享的走没走这个方法15" + "++++++++++++++++");
              var diffHour = 23 + parseInt(app.globalData.startTime.split(":")[0]) - netHour;
              var diffMinute = 59 - netMinte;
              var diffSecond = 60 - netSecond;
              that.startCountDown(diffHour, diffMinute, diffSecond);

              // true隐藏打卡
              that.setData({
                isOnTime: false,
                PuCard: true,
                DoCard: false,
                PaCard: true,
                indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_1.jpg"
                // indexBackgroundImgUrl: "/images/bg_1.jpg"
              });
            } else {
              console.log("看看分享的走没走这个方法16" + "++++++++++++++++");
              that.setData({
                isOnTime: false,
                PuCard: true,
                DoCard: true,
                PaCard: false,
                indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_1.jpg"
                // indexBackgroundImgUrl: "/images/bg_1.jpg"
              });
            }
          }
        } else { // 结束打卡之后的

          if (utoday) { // 参加今天的
            if (!utoday_is_close) { // 没有打卡
              if (!utoday_status) { // 没有显示过打卡失败
                console.log("看看分享的走没走这个方法12" + "++++++++++++++++");
                var there = that;
                app.request("signStatusNo", { call_id: there.data.homeData.todaycallenge.call_id }, true).then(function (data) { });
                that.setData({
                  isOnTime: false,
                  PuCard: true,
                  DoCard: true,
                  PaCard: false,
                  isSucceed: false,
                  isPunchHint: false,
                  indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_1.jpg"
                  // indexBackgroundImgUrl: "/images/bg_1.jpg"
                })
              }
            }
          } // 没有参加今天

          if (utomorrow) { // 参加了明天
            console.log("看看分享的走没走这个方法15" + "++++++++++++++++");
            var diffHour = 23 + parseInt(app.globalData.startTime.split(":")[0]) - netHour;
            var diffMinute = 59 - netMinte;
            var diffSecond = 60 - netSecond;
            that.startCountDown(diffHour, diffMinute, diffSecond);

            // true隐藏打卡
            that.setData({
              isOnTime: false,
              PuCard: true,
              DoCard: false,
              PaCard: true,
              indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_1.jpg"
              // indexBackgroundImgUrl: "/images/bg_1.jpg"
            });
          } else {
            console.log("看看分享的走没走这个方法16" + "++++++++++++++++");
            that.setData({
              isOnTime: false,
              PuCard: true,
              DoCard: true,
              PaCard: false,
              indexBackgroundImgUrl: "https://p021.zhenlutech.com/uploads/template/daka_1.jpg"
              // indexBackgroundImgUrl: "/images/bg_1.jpg"
            });
          }

        }
        console.log("看看分享的走没走这个方法14" + "++++++++++++++++" + utomorrow);
        // 参加了明天

      }


      // 根据事件判断显示打卡的战况
      var showHour = parseInt(app.globalData.endTime.split(":")[0]); // 时
      var showMinte = parseInt(app.globalData.endTime.split(":")[1]); // 分
      var showSecond = showHour * 3600 + showMinte * 60;
      // 公布结果的时间

      var sSecond = showSecond + 1800;
      var shour = parseInt(sSecond / 3600);
      var sminute = parseInt((sSecond % 3600) / 60);
      var resultTime = shour + "时" + sminute + "分";
      that.setData({
        resultTime: resultTime

      });

      if (parseInt(currentTime.split(":")[0]) >= 23 | parseInt(currentTime.split(":")[0]) * 3600 + parseInt(currentTime.split(":")[1]) * 60 < showSecond + 1800) {
        that.setData({ isPunchCardResultHid: true });
      } else {
        that.setData({ isPunchCardResultHid: false });
      }
    })
  },


  initView: function (currentTime) {
    var that = this;
    // 卡片提示的点击事件
    var a = 0;
    that.setData({ isHint: false })
    var timeHint = setInterval(function () {
      a++;
      if (a > 2) {
        that.setData({ isHint: true })
        clearInterval(timeHint);
      }
    }, 200);
  },


  // 将时间转化成秒
  dataChange: function (hour, minute, second) {
    if (hour == 24) {
      hour = 0;
    }
    var secondTime = (hour * 3600) + (minute * 60) + (second * 1);
    return secondTime;
  },

  // 将秒转化成时间
  secondToDayChange: function (second) {
    var hour = parseInt(second / 3600);
    if (hour == 24) {
      hour = 0;
    }
    var minute = parseInt((second % 3600) / 60);
    var second = second % 3600 % 60
    var currentTime = hour + "时" + minute + "分" + second;
    return currentTime
  },

  // 提取开始计时的代码
  startCountDown: function (diffHour, diffMinute, diffSecond) {

    var secondTime = this.dataChange(diffHour, diffMinute, diffSecond);
    this.setData({ secondTime: secondTime });

    var that = this;

    app.globalData.timeInterval = setInterval(function () {
      // 倒计时执行的函数
      if (that.data.secondTime == 0) {
        console.log(that.data.secondTime);
        clearInterval(app.globalData.timeInterval);
        that.setData({
          isOnTime: true,
          PuCard: false,
          DoCard: true,
          PaCard: true
        });
      }
      var daySecond = that.secondToDayChange(that.data.secondTime--);
      // console.log("currentTime:" + daySecond)
      that.setData({ currentTime: daySecond });
    }, 1000);




  },


  // 开机显示的卡片close的点击事件
  modalHintCloseClick: function (event) {
    wx.setStorageSync("modalIsHint", true);
    this.setData({ modalIsHint: true });
  },

  // 开机显示的卡片详细规则的点击事件
  modalHintRuleClick: function (event) {
    console.log("modalHintRuleClick");
    this.gotoChangeContent();
  },

  // 我的战绩的点击事件
  onRecordClick: function (event) {
    console.log("onRecordClick");
    wx.navigateTo({ url: "../mine/mine" })
  },

  // 挑战规则的点击事件
  onChallengeTheRuleClick: function (event) {
    console.log("onChallengeTheRuleClick");
    this.gotoChangeContent();
  },

  //跳转到挑战规则
  gotoChangeContent: function () {
    wx.navigateTo({ url: "../changecontent/changecontent" })
  },

  // 支付一元的点击事件
  onPayClick: function (event) {
    // this.payChangeThing();
  },

  // 再接再厉的点击事件
  onContinueClick: function (event) {
    // this.payChangeThing();
  },

  // 支付一元参加挑战的时间
  payChangeThing: function () {
    var that = this;
    app.request("wxPay", { call_id: that.data.homeData.tomorrowcallenge.call_id,sr_formid: that.data.formEvent}, true).then(function (data) {
     
      if (data.code == 20001) {
        console.log(data.message);
      }else{
        wx.requestPayment({
          timeStamp: data.data.timeStamp,
          nonceStr: data.data.nonceStr,
          package: data.data.package,
          signType: data.data.signType,
          paySign: data.data.paySign,
          success: function (res) {
            app.request("particiCallenge", {}, true).then(function (data) {
              console.log("点击支付的formId" + that.data.formEvent);
              // app.request("participationCall", {}, true).then(function (data) { });
            });
            wx.redirectTo({ url: "../warn/warn" })

          },
          fail: function (res) {
            wx.showToast({
              title: '支付失败',
              image: '/images/icon_fail.png',
              duration: 1000,
              mask: true,
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })
          }
        })
      }
    
    });


  },

  // 打卡倒计时的点击事件
  onPunchCardTimeClick: function (event) {
    console.log("onPunchCardTimeClick");
    this.initView();
  },

  // 打卡邀请的点击事件
  onPunchCardInviteClick: function (event) {
    console.log("onPunchCardInviteClick");
  },

  shareImgClick: function (event) {
    console.log("分享的点击事件");
    this.gotoChangeContent();
  },

  // 分享的方法
  onShareAppMessage: function (res) {
    var that = this;

    return {
      title: "快来加入早起挑战，养成早起习惯还能赚钱！",
      path: "/pages/index/index",

      success: function (res) {
        // 转发成功
        shareTickets: ({

        })
        that.onShow();
      },

      fail: function (res) {
        // 转发失败
        that.onShow();
      }
    }
  },


  // 提交表单
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.formId)
    var event = e.detail.formId;
    this.setData({
      formEvent: event
    })
    this.payChangeThing();
  },

  // 打卡的点击事件
  onPunchCardClick: function () {
    console.log("onPunchCardClick");

    var that = this;

    // 打卡之前需要再判断一下事件
    /************************** */
    app.request("particiCallenge", {}, true).then(function (data) {
      var t = data.data.nowtime;
      var currentTime = t.split(" ")[1];
      // 判断打卡的时间
      console.log();
      if (currentTime >= app.globalData.startTime && currentTime < app.globalData.endTime) {
        var there = that;
        // 调用打卡的接口
        app.request("execSign", { call_id: there.data.homeData.todaycallenge.call_id, sr_formid: there.data.formEvent }, true).then(function (data) {
          there.setData({
            isSucceed: true,
            isPunchHint: false,
            PuCard: true,
            DoCard: true,
            PaCard: false
          });
        });
      } else {
        that.setData({
          isSucceed: false,
          isPunchHint: false,
          PuCard: true,
          DoCard: true,
          PaCard: false
        });
      }
    })
  },

  // 成功卡片的点击事件
  onPunchSucceedClick: function (event) {
    this.setData({
      isPunchHint: true
    });
  },

  // 失败卡片的点击事件
  onPunchfailedClick: function (event) {
    this.setData({
      isPunchHint: true
    });
  },

  // checkbox的监听事件
  checkboxChange: function (event) {
    console.log(event.detail.value);
    if (event.detail.value == "0") {
      this.setData({ isAgree: true });
    } else {
      this.setData({ isAgree: false });
    }
  },

  // 相关协议的点击事件
  onAgreementClick: function (event) {
    console.log("onAgreementClick");
    wx.navigateTo({
      url: "../protocol/protocol",
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  }


})